package rpcTest;

import java.io.*;
import java.lang.reflect.Method;
import java.net.ServerSocket;
import java.net.Socket;

public class server {
    private static boolean running = true;

    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(33322);
        while(running){
            Socket s = ss.accept();
            process(s);
            s.close();
        }
        ss.close();
    }

    private static void process(Socket s) throws Exception{
        InputStream is = s.getInputStream();
        OutputStream os = s.getOutputStream();

        ObjectInputStream ois = new ObjectInputStream(is);

        String clazzName = ois.readUTF();
        String methodName = ois.readUTF();
        Class[] paramTypes = (Class[]) ois.readObject();
        Object[] args = (Object[]) ois.readObject();

        Class clazz = null;
        clazz = UserService.class;

        Method method = clazz.getMethod(methodName,paramTypes);
        Object o = (Object) method.invoke(clazz.newInstance(),args);

        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(o);
        oos.flush();
    }
}
